package com.ruoyi.squadweb.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Player {
    private int id;
    private List<String> onlineIds;
    private String name;
    private int teamId;
    private int squadId;
    private boolean isLeader;
    private String role;

    public Player(int id, List<String> onlineIds, String name, int teamId, int squadId, boolean isLeader, String role) {
        this.id = id;
        this.onlineIds = onlineIds;
        this.name = name;
        this.teamId = teamId;
        this.squadId = squadId;
        this.isLeader = isLeader;
        this.role = role;
    }

    // 省略 getters and setters...

    public static Player fromString(String input) {
        Pattern pattern = Pattern.compile("(ID:\\s*(\\d+))\\s*\\|\\s*(Online\\s*IDs:\\s*(.*?))\\s*\\|\\s*(Name:\\s*(.*?))\\s*\\|\\s*(Team\\s*ID:\\s*(\\d+))\\s*\\|\\s*(Squad\\s*ID:\\s*(\\d+))\\s*\\|\\s*(Is\\s*Leader:\\s*(True|False))\\s*\\|\\s*(Role:\\s*(.*))");
        Matcher matcher = pattern.matcher(input);

        if (matcher.matches()) {
            List<String> ids = new ArrayList<>();
            String eosId = matcher.group(4).replace("EOS:", "").trim();
            String steamId = matcher.group(4).split(" steam: ")[1].trim();
            ids.add("EOS " + eosId);
            ids.add("steam " + steamId);

            return new Player(
                    Integer.parseInt(matcher.group(2)),
                    ids,
                    matcher.group(6),
                    Integer.parseInt(matcher.group(8)),
                    Integer.parseInt(matcher.group(10)),
                    "True".equals(matcher.group(12)),
                    matcher.group(14)
            );
        } else {
            throw new IllegalArgumentException("Invalid input format");
        }
    }

    public static List<Player> parsePlayers(String data) {
        List<Player> players = new ArrayList<>();
        String[] lines = data.split("\n");

        for (String line : lines) {
            if (!line.startsWith("-----")) { // 过滤掉标题行
                Player player = Player.fromString(line);
                players.add(player);
            }
        }

        return players;
    }

    public static void main(String[] args) {
        String data = "----- Active Players -----\n" +
                "ID: 8 | Online IDs: EOS: 0002a4d861664c93b2a51f98063015f3 steam: 76561199406384138 | Name:  18岁纯情男高 | Team ID: 2 | Squad ID: 10 | Is Leader: False | Role: USA_HAT_01\n" +
                "ID: 52 | Online IDs: EOS: 0002ca0c8b9d452b9e73013bae91b31d steam: 76561199040751994 | Name: 山川 2895428459 | Team ID: 1 | Squad ID: 7 | Is Leader: False | Role: RGF_Crewman_01\n" +
                "ID: 90 | Online IDs: EOS: 0002b844be784e128794125f238412a8 steam: 76561198420231607 | Name: kev 903094151 | Team ID: 2 | Squad ID: 5 | Is Leader: True | Role: USA_SL_03\n" +
                "ID: 17 | Online IDs: EOS: 0002985ccbc043f087dd418265fb7b3e steam: 76561199260594319 | Name:  =F.E.N.I.X=小手凉凉 | Team ID: 1 | Squad ID: 4 | Is Leader: True | Role: RGF_SLPilot_01\n" +
                "ID: 34 | Online IDs: EOS: 0002ef4146094f1185774c3635f7f90f steam: 76561198333731625 | Name:  Angela Christina | Team ID: 2 | Squad ID: 3 | Is Leader: True | Role: USA_SLPilot_01\n" +
                "ID: 62 | Online IDs: EOS: 0002783a6db84d708ba9e0422a624038 steam: 76561198122839197 | Name:   BOSS--cike | Team ID: 2 | Squad ID: 10 | Is Leader: False | Role: USA_Rifleman_03\n" +
                "ID: 37 | Online IDs: EOS: 000224fe09e24dfbb011b9c5c89cebc6 steam: 76561198099302145 | Name:  BadRabbits | Team ID: 1 | Squad ID: 11 | Is Leader: True | Role: RGF_SL_01\n" +
                "ID: 92 | Online IDs: EOS: 0002c28cdaa44946ab2b501702c7b561 steam: 76561198421729644 | Name:  CarbineM4A1 | Team ID: 1 | Squad ID: 6 | Is Leader: True | Role: RGF_SL_02\n" +
                "ID: 25 | Online IDs: EOS: 00028d57301449dcb64887de2f4d2ebe steam: 76561199005453133 | Name:  Creastroy | Team ID: 1 | Squad ID: 8 | Is Leader: False | Role: RGF_AR_02\n" +
                "ID: 77 | Online IDs: EOS: 00021388e6b94b2d9f8e586da571496a steam: 76561199075738331 | Name:  Elon Musk | Team ID: 1 | Squad ID: 8 | Is Leader: False | Role: RGF_Grenadier_01\n" +
                "ID: 74 | Online IDs: EOS: 0002d97664cb4ecbac0046c09a48a137 steam: 76561198408369425 | Name:  Explorer | Team ID: 2 | Squad ID: 2 | Is Leader: True | Role: USA_SL_03\n" +
                "ID: 16 | Online IDs: EOS: 0002111481b44a82829ed1d2a2cae4a9 steam: 76561198008979560 | Name:  GeEs | Team ID: 1 | Squad ID: 6 | Is Leader: False | Role: RGF_Engineer_01\n" +
                "ID: 18 | Online IDs: EOS: 0002a435211f48d1aba38a5d2f89c347 steam: 76561198987046562 | Name:  Helz | Team ID: 2 | Squad ID: 9 | Is Leader: False | Role: USA_LAT_02\n" +
                "ID: 79 | Online IDs: EOS: 0002452c135041dbb959eab19bb4ac0e steam: 76561198258158237 | Name:  Himura-Mango | Team ID: 1 | Squad ID: 8 | Is Leader: False | Role: RGF_Medic_02\n" +
                "ID: 11 | Online IDs: EOS: 000250c213ce4cfca7305153d6fd2557 steam: 76561199012067436 | Name:  Linda | Team ID: 1 | Squad ID: 8 | Is Leader: False | Role: RGF_HAT_01\n" +
                "ID: 69 | Online IDs: EOS: 00024fa8a21c4ccabdc565bcb3f88189 steam: 76561199382237435 | Name:  M.YO她说戴了不算第一次 | Team ID: 1 | Squad ID: 3 | Is Leader: True | Role: RGF_SL_03\n" +
                "ID: 56 | Online IDs: EOS: 00024098727e481d9d1a4bc125fba199 steam: 76561198407010734 | Name:  OMGG | Team ID: 2 | Squad ID: 5 | Is Leader: False | Role: USA_LAT_01\n" +
                "ID: 29 | Online IDs: EOS: 00024d69c51d4b5296edd3ec54613c94 steam: 76561199474788955 | Name:  Star | Team ID: 1 | Squad ID: 10 | Is Leader: False | Role: RGF_Rifleman_03\n" +
                "ID: 63 | Online IDs: EOS: 00027c27e9ab4e8aa6a0e0e9cba4e8ea steam: 76561199389832985 | Name:  Zoo➬满山猴子我腚最红 | Team ID: 1 | Squad ID: 3 | Is Leader: False | Role: RGF_Rifleman_03\n" +
                "ID: 6 | Online IDs: EOS: 0002798334b64b248822336d7b73ccee steam: 76561199362512769 | Name:   [KF] 幽  灵 | Team ID: 1 | Squad ID: 6 | Is Leader: False | Role: RGF_Rifleman_03\n" +
                "ID: 110 | Online IDs: EOS: 000205bdce344a46b66725876ee5626a steam: 76561198041298690 | Name:  are YOU ok | Team ID: 2 | Squad ID: 10 | Is Leader: False | Role: USA_AR_02\n" +
                "ID: 2 | Online IDs: EOS: 0002d6b9980b43a4a583d2a6ba17f82a steam: 76561199593205917 | Name:  avzffudyxadhs | Team ID: 2 | Squad ID: 13 | Is Leader: False | Role: USA_Crewman_01\n" +
                "ID: 28 | Online IDs: EOS: 0002480b700443f0b7e2b86ff3d39005 steam: 76561198843139532 | Name:   c.cat | Team ID: 1 | Squad ID: 10 | Is Leader: False | Role: RGF_Medic_02";

        List<Player> players = parsePlayers(data);
        for (Player player : players) {
            System.out.println(player);
        }
    }
}
